import {Injectable} from "@angular/core";
import {CallNumber} from "@ionic-native/call-number";
import {ClientNoteEditProvider} from "../../../providers/client/client-note/client-note-edit/client-note-edit.provider";
import {FollowstateModel} from "../../../model/pages/activity/followstate/followstate.model";
import {FollowstateProvider} from "../../../providers/activity/followstate/followstate";
import {ClientNoteLogModel} from "../../../model/pages/client/client-note/client-note-log/client-note-log.model";
import {PermissionsService} from "../permissions/permissions.service";
import {ToastService} from "../../common/toast.service";
import {Platform} from "ionic-angular";

@Injectable()
export class CallNumberService {

    constructor(private callNumber: CallNumber,
                private clientNoteEditProvider: ClientNoteEditProvider,
                private followstateProvider: FollowstateProvider,
                private permissionsService: PermissionsService,
                private platform: Platform,
                private toastService: ToastService) {

    }

    /**
     * 拨打打话，记录日志，切换活动状态
     * @param {string} phoneNumber
     * @param {ClientNoteLogModel} clientLogModel
     * @param {boolean} isChangeStatus
     * @param {FollowstateModel} followStateModel
     * @param {Function} callBack
     */
    makeCall(phoneNumber: string,
             clientLogModel: ClientNoteLogModel,
             isChangeStatus: boolean,
             followStateModel?: FollowstateModel,
             callBack?: Function,
             noteCallBack?: Function,
             callNumber?: Function) {

        if (this.platform.is('android')) {
            let phonePermission = false;
            this.permissionsService.checkAndroidCallPhonePermission().then(res => {
                phonePermission = res[0];
                console.log('phonePermission ----  ' + phonePermission);
                if (phonePermission) {
                    this.doCall(phoneNumber, clientLogModel, isChangeStatus, followStateModel, callBack, noteCallBack, callNumber);
                } else {
                    this.toastService.showToast('请到“设置>应用”中启用“电话”权限');
                    callNumber();
                }
            }).catch(error => {
                // this.toastService.showToast('请到“设置>应用”中启用“电话”权限');
                callNumber();
            });
        }
        else {
            this.doCall(phoneNumber, clientLogModel, isChangeStatus, followStateModel, callBack, noteCallBack, callNumber);
        }
    }

    private doCall(phoneNumber: string,
                   clientLogModel: ClientNoteLogModel,
                   isChangeStatus: boolean,
                   followStateModel?: FollowstateModel,
                   callBack?: Function,
                   noteCallBack?: Function,
                   callNumber?: Function) {
        // let phonePermission = false;
        //
        // this.permissionsService.checkAndroidCallPhonePermission().then(res => {
        //     phonePermission = res[0];
        //     console.log('phonePermission ----  '+phonePermission);
        // });
        //
        // if(!phonePermission) {
        //     this.toastService.showToast('请至系统设置中开启拨打电话权限');
        //     return;
        // }

        // this.clientNoteEditProvider.saveNote(clientLogModel).subscribe(note => {
        //     console.log('记录日志成功！');
        //     if (isChangeStatus) {
        //         //调用接口
        //         this.followstateProvider.switchFollowState(followStateModel).subscribe(() => {
        //             console.log('切换状态成功！');
        //             if (callBack) {
        //                 callBack();
        //             }
        //             this.baseCall(phoneNumber);
        //         },error3 => {
        //             console.log('切换状态失败！');
        //             this.baseCall(phoneNumber);
        //         });
        //     } else {
        //         if (callBack) {
        //             callBack();
        //         }
        //         this.baseCall(phoneNumber);
        //     }
        //     //
        // },error2 => {
        //     console.log('记录日志失败！');
        //     // this.baseCall(phoneNumber);
        //     if (isChangeStatus) {
        //         //调用接口
        //         this.followstateProvider.switchFollowState(followStateModel).subscribe(() => {
        //             console.log('切换状态成功！');
        //             if (callBack) {
        //                 callBack();
        //             }
        //             this.baseCall(phoneNumber);
        //         },error3 => {
        //             console.log('切换状态失败！');
        //             this.baseCall(phoneNumber);
        //         });
        //     } else {
        //         if (callBack) {
        //             callBack();
        //         }
        //         this.baseCall(phoneNumber);
        //     }
        // });

        //切换状态
        if (followStateModel) {
            this.followstateProvider.switchFollowState(followStateModel).subscribe(() => {
                console.log('切换状态成功！');
                if (callBack) {
                    callBack();
                }
            }, error2 => {
                console.log('切换状态失败！');
            });
        }

        //记日志
        this.clientNoteEditProvider.saveNote(clientLogModel).subscribe(note => {
            console.log('记录日志成功！');
            if (noteCallBack) {
                noteCallBack();
            }
        }, error2 => {
            console.log('记录日志失败！');
        });

        this.baseCall(phoneNumber, callNumber);
    }

    private baseCall(phoneNumber: string, cb ?: Function) {
        if (phoneNumber) {
            this.callNumber.callNumber(phoneNumber, true).then(res => {
                console.log(res);
                console.log('已拨打！');
                setTimeout(() => {
                    cb && cb();
                }, 1000);
            }).catch(() => {
                console.log('没打通！');
                setTimeout(() => {
                    cb && cb();
                }, 1000);
            });
        }
    }

    /**
     * 个险客户日志
     * @param {string} clientNum
     * @returns {ClientNoteLogModel}
     */
    initClientNoteLog(clientNum: string): ClientNoteLogModel {
        let clientLogModel = new ClientNoteLogModel();
        clientLogModel.clientType = 2;
        clientLogModel.noteType = 4;
        clientLogModel.content = '拨打电话';
        clientLogModel.contentType = 1;
        clientLogModel.clientNumber = clientNum;

        return clientLogModel;
    }

    /**
     * 准客户日志
     * @param {string} clientNum
     * @returns {ClientNoteLogModel}
     */
    initCustomerNoteLog(clientNum: string): ClientNoteLogModel {
        let clientLogModel = new ClientNoteLogModel();
        clientLogModel.clientType = 1;
        clientLogModel.noteType = 4;
        clientLogModel.content = '拨打电话';
        clientLogModel.contentType = 1;
        clientLogModel.clientNumber = clientNum;

        return clientLogModel;
    }

    /**
     * 跟踪状态切换
     * @param {string} clientNum
     * @returns {FollowstateModel}
     */
    initFollowStateLog(clientNum: string): FollowstateModel {
        let followLogModel = new FollowstateModel();
        followLogModel.customerId = clientNum;
        followLogModel.activityId = '';

        return followLogModel;
    }
}
